//
//  BanBu_PersonMusicPlay.m
// Halfarea
//
//  Created by banbu3 on 14-4-9.
//
//

#import "BanBu_PersonMusicPlay.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
@implementation BanBu_PersonMusicPlay

/**
 *	@brief	单例
 *
 *	@return	self
 */
+(BanBu_PersonMusicPlay *)sharePlayer

{
    static BanBu_PersonMusicPlay *profilePlayer = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if(profilePlayer == nil)
        {
            profilePlayer = [[self alloc] init];
            [profilePlayer initResource];
        }
    });
    return profilePlayer;
}
/**
 *	@brief	数据初始化
 */
-(void)initResource
{
    _audioPlayer = nil;
    pausing = NO;
}

-(void)setLoadUrl:(NSString *)loadUrl
{
    if(![loadUrl isEqualToString:_loadUrl])
    {
        pausing = NO;
    }

    _loadUrl = [loadUrl copy];
}


/**
 *	@brief	下载music回调
 *
 *	@param 	resDic 	返回值
 *	@param 	error 	错误
 */
- (void)banbuDownloadRequest:(NSDictionary *)resDic didFinishedWithError:(NSError *)error
{
    
    if([[resDic valueForKey:@"ok"]boolValue])
    {
        [self startPlay];
    }
}

/**
 *	@brief	开始播放前的查找是否存在该语音文件不存在将下载后播放
 */
- (void)startPlay
{
    if([FileManager fileExistsAtPath:[AppComManager pathForMedia:_loadUrl]])
    {
        [self play];
    }
    else
    {
        [AppComManager getBanBuMedia:self.loadUrl delegate:self];
    }
    
}

/**
 *	@brief	暂停
 */
- (void)pause
{
    if(_audioPlayer.isPlaying)
    {
        pausing = YES;
        [_audioPlayer pause];
    }
}
- (void)stop
{
    if(_audioPlayer)
    {
        [_audioPlayer stop];
        
    }
    if(_delegate&&[_delegate respondsToSelector:@selector(musicDidEndPlay:)])
    {
        [_delegate musicDidEndPlay:self];
    }
}
- (void)resume
{
    if(_audioPlayer)
    {
        pausing = NO;
        if(_delegate&&[_delegate respondsToSelector:@selector(musicDidStartPlay:)])
        {
            [_delegate musicDidStartPlay:self];
        }
        [_audioPlayer play];
    }
}

- (BOOL)isPause
{
    return pausing;
}

/**
 *	@brief	在界面退出时调用这个方法将所有数据充值
 */
- (void)destoryPlay
{
    if(_audioPlayer)
    {
        [_audioPlayer stop];
        [_audioPlayer release],_audioPlayer = nil;
        self.loadUrl = nil;
        _delegate = nil;
    }
}
/**
 *	@brief	开始播放
 */
- (void)play

{
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"msg_1" ofType:@"mp3"];
    pausing = NO;
    if([_delegate respondsToSelector:@selector(musicDidStartPlay:)])
    {
        [_delegate musicDidStartPlay:self];
    }
    if(_audioPlayer)
    {
        [_audioPlayer release],_audioPlayer = nil;
    }
    NSString *path = [AppComManager pathForMedia:_loadUrl];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    //audio 播放循环设为负数无限循环
    _audioPlayer.numberOfLoops = -1;
    _audioPlayer.delegate = self;
    _audioPlayer.volume = 1.0;
    [_audioPlayer prepareToPlay];
    [_audioPlayer play];
}
/**
 *	@brief	获取是否正在播放
 *
 *	@return	BOOL
 */
- (BOOL)playing
{
    if(_audioPlayer)
        return _audioPlayer.isPlaying;
    else return NO;
}

/**
 *	@brief	delegate接收正在播放的状态
 *
 *	@param 	player 	self
 *	@param 	flag 	未使用的状态
 */

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if([_delegate respondsToSelector:@selector(musicDidEndPlay:)])
    {
        [_delegate musicDidEndPlay:self];
    }
}

@end
